package com.explorati.interview.interview.chapternine;

import java.lang.reflect.Field;

public class FieldTest {
	public static void main(String[] args) {
		FieldTestClass obj1 = new FieldTestClass("hello1", 100);
		FieldTestClass obj2 = new FieldTestClass("hello2", 300);
		System.out.println(compare(obj1, obj2).name + " is bigger");
	}

	public static FieldTestClass compare(FieldTestClass obj1, FieldTestClass obj2) {
		Field field1;
		try {
			field1 = obj1.getClass().getDeclaredField("age");
			Field field2 = FieldTestClass.class.getDeclaredField("age");

			int val1 = (int) field1.get(obj1);
			int val2 = (int) field2.get(obj2);
			if (val1 > val2) {
				return obj1;
			} else {
				return obj2;
			}
		} catch (NoSuchFieldException | SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return null;
	}
}
